package com.bobo.dao.user;

import com.alibaba.druid.util.StringUtils;
import com.bobo.dao.BaseDao;
import com.bobo.model.Photo;
import com.bobo.model.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by bobo on 16-10-28.
 */
public class UserDaoImpl extends BaseDao<User> implements IUserDao {
    @Override
    public Integer create(User user) {
        String sql = "INSERT into t_user(name, sex, birthday, phone, email, address, password, photo_id)" +
                "  VALUE (?,?,?,?,?,?,?,?)";
        return create(sql,user.getName(),user.getSex(),user.getBirthday(),user.getPhone(),
                user.getEmail(),user.getAddress(),user.getPassword(),user.getPhoto().getId());
    }

    @Override
    public boolean upd(User user) {
        String sql = "UPDATE t_user SET name=?, sex=?, birthday=?, phone=?, email=?, address=?," +
                " photo_id=?,status=? WHERE id=?";
        return update(sql,user.getName(),user.getSex(),user.getBirthday(),user.getPhone(),
                user.getEmail(),user.getAddress(),user.getPhoto().getId(),user.getStatus(),
                user.getId());
    }

    @Override
    public boolean updPwd(User user) {
        String sql = "UPDATE t_user SET password=? WHERE id=?";
        return update(sql,user.getPassword(),user.getId());
    }

    @Override
    public User getUser(Integer id) {
        String sql = "SELECT tu.id uid,tu.name,tu.sex,tu.birthday,tu.phone,tu.email,tu.address,tu.status," +
                " tp.id pid,tp.img,tp.status tps " +
                " FROM t_user tu,t_photo tp " +
                " WHERE tu.photo_id=tp.id" +
                " AND tu.id=?";
        User user = new User();
        ResultSet rs = getObject(sql, id);
        try {
            while (rs.next()){
                user.setId(rs.getInt("uid"));
                user.setName(rs.getString("name"));
                user.setSex(rs.getInt("sex"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setStatus(rs.getInt("status"));

                Photo photo = new Photo();
                photo.setId(rs.getInt("pid"));
                photo.setImg(rs.getString("img"));
                photo.setStatus(rs.getInt("tps"));

                user.setPhoto(photo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public Integer getCount(Integer status) {
        String sql = "where status=?";
        return getCount(sql,status);
    }

    @Override
    public List<User> getUsers(Integer current, Integer pageSize, Integer status) {
        String sql = "SELECT tu.id uid,tu.name,tu.sex,tu.birthday,tu.phone,tu.email,tu.address,tu.status, " +
                "tp.id pid,tp.img,tp.status tps FROM t_user tu,t_photo tp WHERE tu.photo_id=tp.id " +
                "AND tu.status=?";
        List<User> users = new ArrayList<>();
        ResultSet rs = pager(sql, current, pageSize, status);
        try {
            while (rs.next()){
                User user = new User();
                user.setId(rs.getInt("uid"));
                user.setName(rs.getString("name"));
                user.setSex(rs.getInt("sex"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setStatus(rs.getInt("status"));

                Photo photo = new Photo();
                photo.setId(rs.getInt("pid"));
                photo.setImg(rs.getString("img"));
                photo.setStatus(rs.getInt("tps"));

                user.setPhoto(photo);
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    @Override
    public User userCompare(String name,String email ,String password) {
        StringBuffer sql =new StringBuffer("SELECT tu.id uid,tu.name,tu.sex,tu.birthday,tu.phone,tu.email,tu.address,tu.status," +
                "      tp.id pid,tp.img ,tp.status tps " +
                " FROM t_user tu,t_photo tp " +
                " WHERE tu.photo_id=tp.id " );


        String account;

        if(name != null) {
            sql.append( " AND tu.name=? ");
            account = name;
        } else {
            sql.append( " AND tu.email=? ");
            account = email;
        }

        sql.append( " AND tu.password=? ");

        ResultSet rs = getObject(sql.toString(),account,password);

        User user = new User();
        try {
            while (rs.next()){
                user.setId(rs.getInt("uid"));
                user.setName(rs.getString("name"));
                user.setSex(rs.getInt("sex"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setStatus(rs.getInt("status"));

                Photo photo = new Photo();
                photo.setId(rs.getInt("pid"));
                photo.setImg(rs.getString("img"));
                photo.setStatus(rs.getInt("tps"));

                user.setPhoto(photo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User updQuery(Integer id, String password) {
        String sql = "SELECT tu.id uid,tu.name,tu.sex,tu.birthday,tu.phone,tu.email,tu.address,tu.status," +
                " tp.id pid,tp.img ,tp.status tps " +
                " FROM t_user tu,t_photo tp " +
                " WHERE tu.photo_id=tp.id " +
                "AND tu.id=? AND tu.password=?";
        ResultSet rs = getObject(sql, id, password);
        User user = new User();
        try {
            while (rs.next()){
                user.setId(rs.getInt("uid"));
                user.setName(rs.getString("name"));
                user.setSex(rs.getInt("sex"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setStatus(rs.getInt("status"));

                Photo photo = new Photo();
                photo.setId(rs.getInt("pid"));
                photo.setImg(rs.getString("img"));
                photo.setStatus(rs.getInt("tps"));

                user.setPhoto(photo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}
